package com.lemonfish.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.vo.JsonResult;
import com.lemonfish.enumcode.CodeEnum;
import com.lemonfish.pojo.entity.Permission;
import com.lemonfish.pojo.vo.PermissionVO;
import com.lemonfish.service.PermissionService;
import com.lemonfish.service.RoleService;
import com.lemonfish.util.MyJsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 权限 相关Controller
 *
 * @author Lemonfish
 * @version 1.0
 * @date 2020-03-02
 * Copyright © ï¿½Å³ï¿½
 */
@Api(tags = {"权限"})
@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController extends BaseCrudMappingRestController<Permission, PermissionVO> {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RoleService roleService;


    @GetMapping("/show/{type}")
    public MyJsonResult getList(@PathVariable("type") String type) {
        System.out.println(type);
        List<Permission> list = permissionService.getEntityList(null);

        if ("list".equals(type)) {
            return MyJsonResult.success(list);
        } else if ("tree".equals(type)) {
            List<Permission> tree = BeanUtils.buildTree(list);
            return MyJsonResult.success(tree);
        } else {
            return MyJsonResult.fail(CodeEnum.FAIL_OPERATION);
        }

    }

    /**
     * 加载更多数据
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取关联数据映射关系")
    @GetMapping("/attachMore")
    public JsonResult attachMore(HttpServletRequest request, ModelMap modelMap) throws Exception {
        Wrapper wrapper = null;
        return new JsonResult(modelMap);
    }
}
